package com.example.testktp;

import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.kkh.tp.ISCChannel;
import com.kkh.tp.ISCServerChannel;
import com.kkh.tp.SCChannel;
import com.kkh.tp.SCTransport;
import com.kkh.tp.SCUdpChannel;

public class TransServerActivity extends Activity implements OnClickListener, ISCServerChannel, ISCChannel {
	
	TpApplication mApp;
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		
		mSvrTp.requestEnd();
		
		mApp = null;
	}


	private static final String tag="svr";
	
	Button btnListen;
	EditText edListenPort;
	TextView tvServerLog;
	Spinner spPeer;
	List<String> peerList;
	
	ArrayAdapter<String> spPeerAdap;
	
	boolean mIsListen;
	SCTransport mSvrTp;
	SCChannel mCh, mUdpCh;
	 
	List<SCChannel> mCnnList;
	LinkedList<String> mMsgList;
	
	
	long mRefTime;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.server_main);
		
		mApp = (TpApplication)getApplication();
		mRefTime = System.currentTimeMillis();
		
		initViews();
	
		mCnnList = new LinkedList<SCChannel>();
		
		mMsgList = new LinkedList<String>();
		
		mSvrTp = new SCTransport(this);
		mSvrTp.start();
		
		putMsg("afsdafsd");
	}
	
	void initViews() {
		TpApplication app = (TpApplication)getApplication();
		
		
		btnListen = (Button)findViewById(R.id.btnListenPort);
		btnListen.setOnClickListener(this);
		
		spPeer = (Spinner)findViewById(R.id.spCnnList);
		peerList = new LinkedList<String>();
		spPeerAdap = new ArrayAdapter(this, android.R.layout.simple_spinner_item, peerList);
		spPeer.setAdapter(spPeerAdap);
		
		int port = app.getCfgServerPort();
		edListenPort = (EditText)findViewById(R.id.edListenPort);
		edListenPort.setText(String.valueOf(port));
		
		tvServerLog = (TextView)findViewById(R.id.tvServerLog);
		
		
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		if(arg0 == btnListen) {
			if(!mIsListen) {
				
				mCh = new SCChannel(mSvrTp);
				String szport = edListenPort.getText().toString();
				int port = Integer.parseInt(szport) ;
				mApp.setCfgServerport(port);
				mCh.requestListen(port);
				putMsg("start listen....port : "+szport);
				mCh.setOnServerListener(this);
				mIsListen = true;
				
				mUdpCh = new SCUdpChannel(mSvrTp);
				mUdpCh.requestBind(port);
				mUdpCh.setOnListener(this);
				//mRefTime = System.currentTimeMillis();
			}
			else {
				mCh.requestClose();
				
				mIsListen = false;
				putMsg("end listen....");
			}
			
			setListenView();
		}
	}
	
	void setListenView() {
		if(mIsListen) {
			btnListen.setText("Stop");
		}
		else {
			btnListen.setText("Start");
		}
	}

	@Override
	public void OnAccept(SCChannel server, SCChannel child) {
		// TODO Auto-generated method stub
		if(server == mCh) {
			Log.d(tag, "new child connection");
			child.setOnListener(this);
			putMsg("new connected : " + child.getRemoteIp());
			mCnnList.add(child);
			peerList.add(child.getRemoteIp());
			
		}
	}
	
	
	void putMsg(String str) {
		
		long t = (System.currentTimeMillis()-mRefTime);
		 
		String tm = String.format("%04d.%03d : %s", t/1000, t%1000, str);
		mMsgList.addFirst(tm);
		String msg="";
		for(String s: mMsgList) {
			msg += s+"\n";
		}
		tvServerLog.setText(msg);
	}

	@Override
	public void OnSCReceive(SCChannel c, byte[] packet, int count) {
		// TODO Auto-generated method stub
		if(c == mUdpCh) {
			putMsg(String.format("Udp received\n\t%s", new String(packet)));
		}
		else
			putMsg(String.format("%s : received\n\t%s", c.getRemoteIp(), new String(packet)));
	}

	@Override
	public void OnSCStatusChanged(SCChannel c, int status) {
		// TODO Auto-generated method stub
		if(c == mUdpCh) {
			
		}
		else {
			if(status == SCTransport.STATUS_DISCONNECTED) {
				putMsg(String.format("%s : disconnected", c.getRemoteIp()));
				c.requestClose();
				peerList.remove(c.getRemoteIp());
			}
		}
	}
	
}
